package com.yuerlive.rainbow.module.login;

import android.Manifest;
import android.content.Intent;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.view.View;


import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseActivity;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.module.login.presenter.LoginContract;
import com.yuerlive.rainbow.module.login.presenter.LoginPresenter;
import com.yuerlive.rainbow.module.main.MainActivity;
import com.yuerlive.rainbow.nim.NimHelper;
import com.yuerlive.rainbow.observer.Observerable;
import com.yuerlive.rainbow.observer.ObserverableType;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.utils.PreferenceUtils;
import com.yuerlive.rainbow.utils.ThirdLoginUtils;
import com.yuerlive.rainbow.utils.UserProxy;
import com.yuerlive.rainbow.widget.timeselector.TextUtil;

import java.util.Date;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Admin on 2017/6/12.
 */

public class LoginWayActivity extends BaseActivity implements LoginContract.View, ThirdLoginUtils.UserLoginInfoListenter, Observerable.ISubscribe {
    LoginContract.Presenter presenter;
    private static final String LOGIN_FLAG = "login";
    @BindView(R.id.loadingView)
    ContentLoadingProgressBar loadingView;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_login_way;
    }

    @Override
    protected void initViews() {
        loadingView.hide();
        setTranslucentStatus(true);
        presenter = new LoginPresenter(this);
        ThirdLoginUtils.getInstance().setListener(this);
        Observerable.getInstance().subscribe(ObserverableType.LOGIN, this);
    }

    @OnClick({R.id.IvWeixin, R.id.IvQQ, R.id.tvMobileLogin})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.IvWeixin:
                loadingView.show();
                ThirdLoginUtils.getInstance().ThridLogin( SHARE_MEDIA.WEIXIN);
                break;
            case R.id.IvQQ:
                if (!checkPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE})) {
                    requestPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0x0002);
                    return;
                }
                loadingView.show();
                ThirdLoginUtils.getInstance().ThridLogin( SHARE_MEDIA.QQ);
                break;
            case R.id.tvMobileLogin:
                jumpToActivity(LoginActivity.class);
                break;
        }
    }

    @Override
    public void setLoginInfo(Map<String, String> data, SHARE_MEDIA platform) {
        String icon = data.get("iconurl");
        String nickname = data.get("name");
        String sex = data.get("gender");
        if ("男".equals(sex)) {
            sex = String.valueOf(0);
        } else {
            sex = String.valueOf(1);
        }
        String uid = data.get("uid");
        String openId = platform == SHARE_MEDIA.QQ ?data.get("openid"):data.get("unionid");
        String platform2 = platform == SHARE_MEDIA.QQ ? String.valueOf(1) : String.valueOf(2);
        presenter.thirdLogin(icon, nickname, openId, platform2, sex, String.valueOf(1));
    }

    @Override
    public void hideProgressBar() {
        L.e(TAG,"hideProgressBar");
        loadingView.hide();
    }

    @Override
    public void showImgCode(String imgUrl) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void loginResult(User user) {
        if (user == null) {
            showToast("登录失败");
        } else {
            showToast("登录成功");
            UserProxy.setUser(user);
            initDaily();
//            NimHelper.login(NimHelper.generateLoginInfo(user.getUserId(), user.getUserId()));
            /*if (user.getFirstLogin() == 0) {
                jumpToActivity(MainActivity.class);
            } else {
                jumpToActivity(CompleteInfoActivity.class);
            }*/
            jumpToActivity(MainActivity.class);
            finish();
        }
    }

    @Override
    public void sendSuccess(BaseData baseData) {

    }

    @Override
    public void bindMobileSuccess(BaseData baseData) {

    }

    @Override
    public void showFail(String message, String api) {

    }

    @Override
    public void setPresenter(LoginContract.Presenter presenter) {
        super.presenter = presenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ThirdLoginUtils.getInstance().removeListener();
        Observerable.getInstance().unSubscribe(ObserverableType.LOGIN,this);
        if (loadingView!=null)
        loadingView.hide();
    }

    @Override
    public <T> void update(ObserverableType key, T data) {
        switch (key) {
            case LOGIN:
                finish();
                break;
        }
    }

    @Override
    public void loginFaild(int code, String result) {
        showSnackbar(getRootView(mActivity),result,0);
    }

    private void initDaily(){
        long current = System.currentTimeMillis();
        String prefix = UserProxy.getUser().getUserId();
        if (PreferenceUtils.getLong(prefix + "_START_TIME") <= 0) {
            PreferenceUtils.putLong(prefix + "_START_TIME", current);

        } else {
            Date date = new Date(PreferenceUtils.getLong(prefix + "_START_TIME"));
            Date currentDate = new Date(current);
            if (date.getDate() == currentDate.getDate() && date.getMonth() == currentDate.getMonth()) { //同一天
                //PreferenceUtils.putLong("START_TIME", current);
            } else {
                PreferenceUtils.putLong(prefix + "_START_TIME", current);  //新的一天
            }
        }
    }
}
